package utils;
import javax.swing.JTable;

import org.scilab.modules.javasci.JavasciException;
import org.scilab.modules.javasci.Scilab;
import org.scilab.modules.javasci.JavasciException.InitializationException;
import org.scilab.modules.types.ScilabType;



/** 
 * 
 *SciUtils
 *auther:
 * @version 
 * 
 */

public class SciUtils {
	/*
	 * ����scilab ִ��
	 * param table.toString 
	 * 
	 * return String[][]
	 * */
	public static String[][] sciEngin(String tablemodel) {
		//String send = StringUtils.format4Table2sci(tablemodel);
		String[][] recive = null;
		ScilabType scitype = null;
		Scilab sci;
		try {
			sci = new Scilab();

			if (sci.open()) {
				sci.exec(tablemodel);
				if(sci.isExistingVariable(StringUtils.BEGIN)){
					scitype = sci.get(StringUtils.BEGIN);
					System.out.println("scitype:"+scitype.toString());
					recive = StringUtils.format4sci2table(scitype.toString(),
					scitype.getHeight(), scitype.getWidth());
				}
				sci.close();
			}

		} catch (InitializationException e) {
			e.printStackTrace();
		} catch (JavasciException e) {
			e.printStackTrace();
		}

		return recive;

	}
	/**
	 * @param String table , int operation
	 * 
	 * @return String[][]
	 * */
	public static String[][] sciEngin(String table , int operation) {
		String[][] recive = null;
		ScilabType scitype = null;
		Scilab sci;
		String ope="";
		try {
			sci = new Scilab();
			if (sci.open()) {
				sci.exec(table);
				System.out.println(operation);
				switch(operation){
					case 0: //求逆
						ope=StringUtils.END+"="+"inv("+StringUtils.BEGIN+")";
						sci.exec(ope);
						if(sci.isExistingVariable(StringUtils.END)){
							scitype = sci.get(StringUtils.END);
							recive = StringUtils.format4sci2table(scitype.toString(),
							scitype.getHeight(), scitype.getWidth());
						}
						break;
					case 1://求秩
						ope=StringUtils.END+"="+"rank("+StringUtils.BEGIN+")";
						sci.exec(ope);
						if(sci.isExistingVariable(StringUtils.END)){
							scitype = sci.get(StringUtils.END);
							recive = new String[1][1];
							recive[0][0] = scitype.toString();
						}
						break;
					case 2://转置
						ope=StringUtils.END+"="+StringUtils.BEGIN+"'";
						break;
				}
				
				if(sci.isExistingVariable(StringUtils.END)){
					scitype = sci.get(StringUtils.END);
					recive = StringUtils.format4sci2table(scitype.toString(),
					scitype.getHeight(), scitype.getWidth());
				}
				sci.close();
			}

		} catch (InitializationException e) {
			e.printStackTrace();
			MessageBox.Show("初始化Scilab失败！请检查javasci运行环境。");
		} catch (JavasciException e) {
			e.printStackTrace();
			MessageBox.Show("Scilab执行失败！程序出错。");
		}

		System.out.println(recive);
		return recive;
	}

	public static boolean checkdata(JTable table){
		int hang = table.getRowCount();
		int lie = table.getColumnCount();
		for (int i = 0; i < hang; i++) {
			//��
			for (int j = 0; j < lie; j++) {
				String 	value = table.getModel().getValueAt(i, j).toString();
				if (value== null||!StringUtils.isNumber(value)) {
					int action = MessageBox.Show("是否自动将非数字位置为'0'?", 12);
					switch(action ) {
						case -1:
							table.selectAll();
							return false;
						case 0:
							table.getModel().setValueAt(0, i, j);
							break;
						case 2:
							table.selectAll();
							return false;
						default:
							break;
					}
				} 
			
			
			}
			
		}
		return true;
	}
	/*
	 * 
	 * Scitype2Vector;
	 * 
	 * */

}
